//
//  ErWeiMaViewController.m
//  1243
//
//  Created by ym on 16/4/16.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ErWeiMaViewController.h"
#import "ReadLoginData.h"
@interface ErWeiMaViewController ()
@property (nonatomic, strong) UIImage *QRImage;
@property (nonatomic, copy) NSString *loginUsername;
@end

@implementation ErWeiMaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self data];
    self.navigationItem.title = @"个人信息";
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"upMusic"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
//    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    UILabel *accountLabel = [[UILabel alloc] init];
     NSString *str = [NSString stringWithFormat:@"%@", [ ReadLoginData readLoginData][@"UserName"]];
    accountLabel.text = [NSString stringWithFormat:@"账号:%@", str];
    accountLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:accountLabel];
    [accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(90);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(50);
        
    }];
    UIView *QRView = [UIView new];
    [self.view addSubview:QRView];
    [QRView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(accountLabel.mas_bottom).offset(20);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.view.mas_width);
    }];
    [self QRImage1];
    UIImageView *QRImageView = [[UIImageView alloc] initWithImage:self.QRImage];
    [QRView addSubview:QRImageView];
    [QRImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(QRView);
        make.size.mas_equalTo(CGSizeMake(250, 250));
    }];
    
    UILabel *tipsLabel = [UILabel new];
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    tipsLabel.text = @"扫一扫上方二维码付款";
    [self.view addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.equalTo(QRView);
        make.height.equalTo(@25);
        make.top.equalTo(QRView.mas_bottom).offset(30);
        
    }];
    
}

- (void)data{
//  [ ReadLoginData readLoginData]
    NSString *str = [NSString stringWithFormat:@"%@", [ ReadLoginData readLoginData][@"Number"]];
    self.loginUsername = str;
    NSLog(@"str%@",str);
//    [ReadLoginData readLoginDataWithPathPlistString:<#(NSString *)#>]
    
}

- (void)QRImage1
{

    NSData *stringData = [self.loginUsername dataUsingEncoding:NSUTF8StringEncoding];
    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //设置内容和纠错级别
    [qrFilter setValue:stringData forKey:@"inputMessage"];
    [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];

    UIColor *onColor = [UIColor blackColor];
    UIColor *offColor = [UIColor whiteColor];

    CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                       keysAndValues:
                             @"inputImage",qrFilter.outputImage,
                             @"inputColor0",[CIColor colorWithCGColor:onColor.CGColor],
                             @"inputColor1",[CIColor colorWithCGColor:offColor.CGColor],
                             nil];
    
    CIImage *qrImage = colorFilter.outputImage;

    CGSize size = CGSizeMake(300, 300);
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    _QRImage = codeImage;
    
}
#pragma mark - 返回
- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/**
 *
 *
 *  @return 
 */
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
